<template>
  <div class="user-management-container">
    <el-form ref="form" label-width="200px" :model="form" :rules="rules">
      <el-form-item label="幻灯片名称" prop="slideShowName">
        <el-input v-model="form.slideShowName" placeholder="请输入幻灯片名称" />
      </el-form-item>

      <el-form-item label="幻灯片图片" prop="slideShowUrl">
        <el-input v-model="form.slideShowUrl" style="display: none" />
        <el-upload
          v-model="form.slideShowUrl"
          action="https://star.gengduoke.com/adminapi/file/commonupload"
          :before-upload="beforeAvatarUpload"
          class="avatar-uploader"
          :headers="{
            'X-Token': token,
          }"
          :on-success="handleAvatarSuccess"
          :show-file-list="false"
        >
          <img v-if="imageUrl" class="avatar" :src="imageUrl" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="链接类型" prop="slideShowType">
        <el-radio v-model="form.slideShowType" label="0">菜单</el-radio>
        <el-radio v-model="form.slideShowType" label="1">页面</el-radio>
        <el-radio v-model="form.slideShowType" label="2">h5</el-radio>
      </el-form-item>
      <el-form-item label="链接" prop="skipPath">
        <el-input v-model="form.skipPath" placeholder="请输入跳转路径" />
      </el-form-item>

      <el-form-item label="状态" prop="block">
        <el-radio v-model="form.block" label="0">停用</el-radio>
        <el-radio v-model="form.block" label="1">启用</el-radio>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" placeholder="请输入排序" />
      </el-form-item>
    </el-form>
    <el-button class="baocun" size="medium" type="primary" @click="save">
      保存
    </el-button>
  </div>
  <!-- 增加 -->
</template>

<script>
  /* eslint-disable*/
  import { doAdd } from '@/api/lunbotu'
  import {
    provinceAndCityData,
    regionData,
    provinceAndCityDataPlus,
    regionDataPlus,
    CodeToText,
    TextToCode,
  } from 'element-china-area-data'
  //import { number } from 'echarts'
  export default {
    name: 'search',

    data() {
      const validatesurfacePlot = (rule, value, callback) => {
        console.log(this.form.slideShowUrl)
        if (this.form.slideShowUrl === '') {
          callback(new Error('请添加图片!!!!'))
        } else {
          callback()
        }
      }
      return {
        options: regionDataPlus,
        selectedOptions: [],
        token: '',
        imageUrl: '',
        value: true,
        value1: true,
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
        options1: [],
        options2: [],
        options3: [],
        valueone: '',
        valuetwo: '',
        form: {
          sort: 0,
          slideShowUrl: '', //图片
        },
        activated() {
          if (this.$refs.MaskVideo.src) this.$refs.MaskVideo.play()
        },
        deactivated() {
          if (this.$refs.MaskVideo.src) this.$refs.MaskVideo.pause()
        },

        rules: {
          slideShowName: [
            { required: true, trigger: 'blur', message: '请输入轮播图名称' },
          ],
          ofEnterprise: [
            {
              required: true,
              trigger: 'change',
              message: '请输入所属协作公司',
            },
          ],
          skipPath: [
            { required: true, trigger: 'change', message: '请输入跳转路径' },
          ],
          slideShowUrl: [
            { required: true, trigger: 'change', message: '请添加图片' },
            { validator: validatesurfacePlot, trigger: 'change' },
          ],
          enterpriseName: [
            { required: true, message: '请输入企业名称', trigger: 'blur' },
          ],
          sort: [{ pattern: /^(\d+)((?:\.\d+)?)$/, message: '请输入合法数字' }],
          cityId: [
            { required: true, trigger: 'change', message: '请选择城市' },
          ],
          block: [{ required: true, trigger: 'change', message: '请选择' }],
          slideShowType: [
            { required: true, trigger: 'change', message: '请选择' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.token = this.$store.getters['user/token']
    },
    methods: {
      handleAvatarSuccess(res, file, fileList) {
        this.imageUrl = URL.createObjectURL(file.raw)
        console.log(res)
        this.form.surfacePlot = res.data
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return true
      },
      //选择区域

      handleAvatarSuccess(res, file, fileList) {
        this.imageUrl = URL.createObjectURL(file.raw)
        console.log(res)
        this.form.slideShowUrl = res.data
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return true
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          this.imageUrl = row.slideShowUrl
        }
        this.dialogFormVisible = true
      },
      close() {
        let userId = this.form.userId
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        if (!this.form.id) {
          this.form.userId = userId
        }
        console.log(this.form)
        this.dialogFormVisible = false
        this.imageUrl = ''
        // this.form = ''
      },
      save() {
        // this.$router.push('/other/workflow')

        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              const msg = doEdit(this.form)
              console.log(msg)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              this.$emit('fetch-data')
              this.close()
            } else {
              const msg = doAdd(this.form)

              this.$emit('fetch-data')
              this.close()
              msg.then((res) => {
                console.log(res.msg)
                if (res.msg === '添加成功') {
                  this.$baseMessage(
                    res.msg,
                    'success',
                    'vab-hey-message-success'
                  )
                  this.$router.push({ path: '/setting/slide/slidelist' })
                }
              })
            }
          }
        })
      },
    },
  }
</script>
<style>
  /* .huakuai {
    position: relative;
    left: 96%;
  } */
  .avatar-uploader .el-upload {
    position: relative;
    overflow: hidden;
    cursor: pointer;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    width: 178px;
    height: 178px;
    font-size: 28px;
    line-height: 178px;
    color: #8c939d;
    text-align: center;
  }
  .avatar {
    display: block;
    width: 178px;
    height: 178px;
  }
  .baocun {
    width: 100%;
    height: 100%;
  }
</style>
